package com.getpebble.android.config;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.getpebble.android.common.core.async.PblAsyncTask;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblContentProviderUtil;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.core.util.PblPreferences.PrefKey;
import com.getpebble.android.http.HttpRequestUtil;
import com.google.gson.JsonObject;
import com.koushikdutta.ion.Response;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

public class SyncBootConfig {
    private static final String TAG = SyncBootConfig.class.getSimpleName();

    private static class BootConfigSyncTask extends PblAsyncTask {
        private Context mContext;
        private String mCustomBootConfigUrl;
        private Runnable mOnBootConfigUpdated;

        private BootConfigSyncTask(String customBootconfigUrl, Context context, Runnable onBootConfigUpdated) {
            this.mContext = context;
            this.mCustomBootConfigUrl = customBootconfigUrl;
            this.mOnBootConfigUpdated = onBootConfigUpdated;
        }

        public boolean doInBackground() {
            return SyncBootConfig.syncBootConfig(this.mContext, this.mCustomBootConfigUrl);
        }

        public void onTaskSuccess() {
            if (this.mOnBootConfigUpdated != null) {
                this.mOnBootConfigUpdated.run();
            }
        }

        public void onTaskFailed() {
        }
    }

    public static synchronized boolean syncBootConfig(Context context, String customBootConfigUrl) {
        boolean z;
        synchronized (SyncBootConfig.class) {
            Trace.debug(TAG, "syncBootConfig()");
            try {
                Response<JsonObject> response = HttpRequestUtil.fetchBootConfig(context, customBootConfigUrl, 8000);
                try {
                    Trace.debug(TAG, "bootconfig response");
                    int responseCode = response.getHeaders().getResponseCode();
                    if (responseCode != 200) {
                        Trace.error(TAG, "Error fetching bootconfig data, response code: " + responseCode);
                        z = false;
                    } else {
                        Cursor cursor = context.getContentResolver().query(PblContentProviderUtil.getTableUri("boot_config"), null, null, null, null);
                        if (cursor != null) {
                            int count = cursor.getCount();
                            cursor.close();
                            ContentValues contentValues = new ContentValues();
                            contentValues.put("config_json", ((JsonObject) response.getResult()).toString());
                            Trace.debug(TAG, "bootconfig cursor getcount = " + count);
                            if (count == 0) {
                                context.getContentResolver().insert(PblContentProviderUtil.getTableUri("boot_config"), contentValues);
                            } else {
                                context.getContentResolver().update(PblContentProviderUtil.getTableUri("boot_config"), contentValues, null, null);
                            }
                        }
                        Trace.debug(TAG, "Done bootconfig sync");
                        z = true;
                    }
                } catch (ExecutionException e) {
                    Trace.error(TAG, "Failed to sync boot config.", e);
                    z = false;
                } catch (InterruptedException e2) {
                    Trace.error(TAG, "Failed to sync boot config.", e2);
                    z = false;
                }
            } catch (TimeoutException e3) {
                Trace.error(TAG, e3.getMessage(), e3);
                z = false;
            }
        }
        return z;
    }

    public static void syncBootConfigIfRequired(String customBootconfigUrl, Context context, Runnable onBootConfigUpdated, boolean force) {
        PblPreferences prefs = new PblPreferences(context);
        boolean lastSyncExpired = System.currentTimeMillis() - prefs.getLongData(PrefKey.LAST_BOOTCONFIG_SYNC_MILLIS, -1) >= 43200000;
        if (force || lastSyncExpired) {
            prefs.setLongData(PrefKey.LAST_BOOTCONFIG_SYNC_MILLIS, System.currentTimeMillis());
            new BootConfigSyncTask(customBootconfigUrl, context, onBootConfigUpdated).submit();
            return;
        }
        Trace.debug(TAG, "Not syncing BootConfig - too soon since last sync");
    }
}
